window.Control = function()
{
    this.M = null;
};

Control.prototype = {

    /** 设置当前数据*/
    setItem : function( item )
    {
        this.M.setData( item );
    },

    /** 获取数据模型初始数据*/
    getItem : function()
    {
        return this.M.getData();
    },

    setWhereItem : function( item )
    {
        this.M.setCondition( item );
    },

    getWhereItem : function()
    {
        return this.M.getListConditionData();
    },

    /** 计算显示元素个数*/
    getElementsLength : function( maxWidth, elementsSize, padding )
    {
        return Math.floor( maxWidth/( elementsSize.width + padding ) );
    },

    /** 计算位置*/
    getPosition : function( elementLength, maxWidth, elementsSize, padding  )
    {
        var length = this.getElementsLength(  maxWidth, elementsSize, padding );

        var rowLength = Math.ceil( elementLength/length );

        var posArray = [];

        for( var x = 0; x < rowLength; x += 1 )
        {
            for( var y = 0; y < length; y += 1 )
            {
                posArray.push(
                    {
                        x : y*( elementsSize.width + padding ),
                        y : x*( elementsSize.height + padding )
                    }
                );
            }
        }

        return posArray;
    },

    /** 获取数据*/
    get : function( url, data,callbackObject, type )
    {
        this.H.Request( "get", url, data, callbackObject, type || "json" );
    },
    /** 保存数据*/
    post : function( url, data,callbackObject, type )
    {
        return this.H.Request( "post", url, data, callbackObject, type || null );
    },
    /** 输出数据到模板*/
    printDataToMapPanel : function( data, template )
    {
        return this.H.getTemplate( template, data );
    },

    select2ChangeHandle : function( e, formGroup, View )
    {
        var data = {};

        for( var i = 0, f; f = formGroup[ i ]; i += 1 )
        {
            var selects = f.getElementsByTagName("select")[ 0 ];

            if( selects )
            {
                var sName = f.getAttribute("data-name");

                var result = View.$( f.getElementsByTagName("select") ).select2("val");

                if( result.length ) data[ sName ] = result;
            }
        }

        return data;
    },
    // 输入提示
    enterTips : function( _this, sMin, sMax, inputs, toastrWarning )
    {

        if( _this.getAttribute("data-tag") == "time" )
        {
            sMin = inputs[ 0 ].value;
            sMin = sMin ? new Date( sMin.split("-")[ 0 ], sMin.split("-")[ 1 ], sMin.split("-")[ 2 ]).getTime() : "";
            sMax = inputs[ 1 ].value;
            sMax = sMax ? new Date( sMax.split("-")[ 0 ], sMax.split("-")[ 1 ], sMax.split("-")[ 2 ] ).getTime() : "";
        }

        if(sMin==""&&sMax=="")
        {
            toastrWarning("必须输入数字，请重新输入！");

            inputs[ 0 ].focus();

            return true;
        }

        if( (sMin!=""&&parseFloat(sMin)!=sMin) || (sMax!=""&&parseFloat(sMax)!=sMax ) )
        {
            toastrWarning("必须输入数字，请重新输入！");

            inputs[ 0 ].value = "";

            inputs[ 0 ].focus();

            inputs[ 1 ].value = "";

            //inputs[ 1 ].focus();

            return true;
        }
        if( sMin&&sMax&&( parseFloat(sMax ) < parseFloat(sMin ) ) )
        {
            toastrWarning("请输入正确的范围值！");

            inputs[ 1 ].value = "";

            inputs[ 1 ].focus();

            return true;
        }

        return false;
    },
    // 获取范围
    getCondRange : function( _this, sMin, sMax, sUon )
    {
        var psMin = sMin;
        var psMax = sMax;

        if( _this.getAttribute("data-tag") == "time" )
        {
            sMin = sMin ? sMin.split("-")[ 0 ] + "年" + sMin.split("-")[ 1 ] + "月" + sMin.split("-")[ 2 ] + "日" : "";

            sMax = sMax ? sMax.split("-")[ 0 ] + "年" + sMax.split("-")[ 1 ] + "月" + sMax.split("-")[ 2 ] + "日" : "";
        }

        var resValue = "";

        if( sMin && sMax )
        {
            resValue = sMin + sUon + " - " + sMax + sUon;
        }
        else if( sMin && !sMax )
        {
            resValue = sMin + sUon + " 以上";
        }
        else if( !sMin && sMax )
        {
            resValue = sMax + sUon + " 以下";
        }

        return {
            string : resValue,
            max : psMax,
            min : psMin
        }
    },
    // 重置条件数据
    resetConditionItem : function()
    {
        this.setTemplateConditionItem( [] );

        this.setConditionItem( {} );
    },

    setTemplateConditionItem : function( item )
    {
        this.M.setTemplateListConditionData( item );
    },
    setConditionItem : function( item )
    {
        this.M.setListConditionData( item );
    },

    // 设置条件数据
    setCondDataItem : function( templateConditonData, conditionData, sName, sTitle, sType )
    {
        // 获取模板条件数据模型
        var tmplateCondtion = this.getTemplateConditionItem() || [];
        // 获取条件数据模型
        var condtion = this.getConditionItem() || {};

        templateConditonData["id"] = "con_" + sName;

        templateConditonData["type"] = sType;

        templateConditonData["title"] = sTitle;

        tmplateCondtion = this.replaceSameData( tmplateCondtion, templateConditonData, "id" );

        this.setTemplateConditionItem( tmplateCondtion );

        condtion[ sName ] = conditionData;

        this.setConditionItem( condtion );
    },
    getTemplateConditionItem : function()
    {
        return  this.M.getTemplateListConditionData();
    },
    getConditionItem : function()
    {
        return  this.M.getListConditionData();
    },

    // 替换重复数据到数组
    replaceSameData : function( beforedata, newdata, name, value )
    {
        if( !( name && newdata[ name ] ) ) return beforedata;

        for( var i = 0, f; f = beforedata[ i ]; i += 1 )
        {
            if( f[ name || "id" ] == newdata[ name || "id" ] )
            {
                beforedata.splice( i , 1 );

                break;
            }
        }

        newdata[ value || "value" ] && beforedata.push( newdata );

        return beforedata;
    },
    /** 输出数据到模板*/
    printDataToTemplate : function( data, template )
    {
        return this.H.getTemplate( template, data );
    },
    // 删除条件数据
    removeConditionItem : function( id )
    {
        if( !id ) return false;

        var sId = id.substring( 4 );
        // 获取模板条件数据模型
        var tmplateCondtion = this.getTemplateConditionItem() || [];
        // 获取条件数据模型
        var condtion = this.getConditionItem() || {};

        condtion[ sId ] = null;

        delete condtion[ sId ];

        for( var i = 0, f; f = tmplateCondtion[ i ]; i += 1 )
        {
            if( f["id"] == id )
            {
                tmplateCondtion.splice( i , 1 );

                break;
            }
        }

        this.setTemplateConditionItem( tmplateCondtion );


        this.setConditionItem( condtion );

        return true;
    },


};
